/*
 * Copyright (C) 2013 Winson Wong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package myidea.winson.android.cangjieime;

import java.util.ArrayList;

import android.util.Log;

public class CangjieComposing {
	
	StringBuilder mStr;
	ArrayList<int[]> mCharList;
	ArrayList<float[]> mCharPossibleList;

	public CangjieComposing(CharSequence str){
		mStr = new StringBuilder(str);
		for(int i=0;i<str.length();i++){
			mCharList.add(new int[0]);
			mCharPossibleList.add(new float[0]);
		}
	}
	
	public CangjieComposing(){
		mStr = new StringBuilder();
		mCharList =  new ArrayList<int[]>();
		mCharPossibleList = new ArrayList<float[]>();
	}
	

	public int length(){
		//return mStr.length();
		return mCharList.size();
	}
	
	
	public String substring(int start, int end){
		return mStr.substring(start, end);
	}
	
	
	public void append(char c, int[] subChar, float[] subCharPossible){
		mCharList.add(subChar);
		mCharPossibleList.add(subCharPossible);
		mStr.append(c);
	}
	
	
	public String toString(){
		return mStr.toString();
	}
	
	
	public CangjieComposing delete(int start, int end){
		mStr.delete(start, end);
		for(int i=end-1; i>=start; i--){
			mCharList.remove(i);
			mCharPossibleList.remove(i);
		}
		return this;
	}
	
	public void clear(){
		mStr.setLength(0);
		mCharList.clear();
		mCharPossibleList.clear();
	}
	
	public void setLength(int length){
		mStr.setLength(length);
		for (int i=mCharList.size()-1;i>=length;i--){
			mCharList.remove(i);
			mCharPossibleList.remove(i);
		}
	}
	
	public char charAt(int index){
		return mStr.charAt(index);
	}
	
	public String[] getAllComposing(){
		ArrayList<CharTreeNode> mostBottomNodeList = null;
		int maxComposeCountIn = 5;
		for(int[] chars : mCharList){
			ArrayList<CharTreeNode> nextBottomNodeList = new ArrayList<CharTreeNode>();
			for(int c : chars){
				if (c == -1) break;
				if(mostBottomNodeList == null){
					CharTreeNode node = new CharTreeNode(c,null);
					nextBottomNodeList.add(node);
				}
				else{
					for(CharTreeNode pNode : mostBottomNodeList){
						CharTreeNode node = new CharTreeNode(c,pNode);
						nextBottomNodeList.add(node);
					}					
				}
			}
			mostBottomNodeList = nextBottomNodeList;
			if (--maxComposeCountIn <= 0){
				break;
			}
		}

		String[] returnStr = new String[mostBottomNodeList.size()];
		int index = 0;
		for(CharTreeNode node : mostBottomNodeList){
			returnStr[index++] = node.toString();
			//Log.i("CangjieComposing",returnStr[index-1]);
		}
		
		
		return returnStr;
	}

	
	private class CharTreeNode{
		int mCharCode;
		CharTreeNode mParent;
		
		public CharTreeNode(int charCode, CharTreeNode parent){
			mCharCode = charCode;
			mParent = parent;
		}
		
		public String toString(StringBuilder str){
			StringBuilder str_i;
			if (str == null){
				str_i = new StringBuilder();
			}
			else{
				str_i = str;
			}
			
			if (mParent != null){
				mParent.toString(str_i);
			}
			str_i.append((char) mCharCode);
			
			if (str == null){
				return str_i.toString();
			}
			else{
				return null;
			}
		}
		
		public String toString(){
			return toString(null);
		}
	}
	
}
